package top.lingkang.securityoauth2.client.config;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import top.lingkang.securityoauth2.client.filter.FinalSecurityFilter;
import top.lingkang.securityoauth2.client.http.FinalSecurityContext;
import top.lingkang.securityoauth2.client.http.SecurityOauth2Holder;

import javax.annotation.PostConstruct;
import javax.servlet.Filter;

/**
 * @author lingkang
 * Created by 2022/7/1
 */
public class Oauth2ClientConfiguration implements ApplicationRunner {
    private Oauth2ClientProperties properties = new Oauth2ClientProperties();

    @PostConstruct
    private void init() {
        config(properties);

        // 配置设置到上下文中！
        FinalSecurityContext.init(properties);
    }

    protected void config(Oauth2ClientProperties properties) {
    }

    // start ---------------
    @Override
    public void run(ApplicationArguments args) throws Exception {

    }

    // bean ------------------
    @Bean
    @Order(1)
    public Oauth2ClientProperties finalSecurityProperties() {
        return properties;
    }

    @Bean(name = "oauth2BeanFilter")
    @Order(2)
    public Filter oauth2BeanFilter() {
        return new FinalSecurityFilter(properties);
    }

    @Bean
    @Order(10)
    public SecurityOauth2Holder securityOauth2Holder(){
        return new SecurityOauth2Holder();
    }
}
